




//
//  NIndustryDataInqueryDetailVC.m
//  nncloud
//
//  Created by macfai on 2018/12/21.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NIndustryDataInqueryDetailVC.h"
#import "NDataInqView.h"
#import "NInqueryModel.h"
@interface NIndustryDataInqueryDetailVC ()<UITableViewDataSource,UITableViewDelegate,ITDatePickerControllerDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)NSArray *titleArray;

@property(nonatomic,strong)NSArray *pickerArray;

@property(nonatomic,strong)UILabel *startLabel;
@property(nonatomic,strong)UILabel *endLabel;

@property (nonatomic,assign)NSInteger index;

@property (nonatomic,copy)NSString *startTime;
@property (nonatomic,copy)NSString *endTime;

@property (nonatomic,copy)NSString *output;


@property(nonatomic,strong)UILabel *sumLabel;
@property(nonatomic,strong)UILabel *sellLabel;
@property(nonatomic,strong)UILabel *exportLabel;


@end

@implementation NIndustryDataInqueryDetailVC

#define cellIdentifier @"datai"
#define SectionHeaderHeight  45.f

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 55.f;
        _tableView.backgroundColor = kBackgroundColor;
        //        [_tableView registerNib:[UINib nibWithNibName:@"NZhaopinCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
//        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        //        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"工业数据查询" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    //    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 40) title:@"" Image:@"搜索" selectedIMG:nil tartget:self action:@selector(search)];
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    [self addHeader];
//    [self loadData];
    [self inqData];
    // Do any additional setup after loading the view.
}

- (void)addHeader{
    //自定义xib view  需要生成xib 后将这个class  类指向xib 就可了
    NDataInqView *header = [[[NSBundle mainBundle] loadNibNamed:@"NDataInqView" owner:self options:nil] lastObject];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(startAction)];
    [header.startLabel addGestureRecognizer:tap];
    self.startLabel = header.startLabel;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(endAction)];
    [header.endLabel addGestureRecognizer:tap1];
    self.endLabel = header.endLabel;
    self.tableView.tableHeaderView = header;
    
    self.sumLabel = header.sumLabel;
    self.sellLabel = header.sellLabel;
    self.exportLabel = header.exportLabel;
}

- (void)startAction{
    YYLog(@"开始");
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 610;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    [self presentViewController:datePickerController animated:YES completion:nil];
}

- (void)endAction{
    YYLog(@"结束");
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 611;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    [self presentViewController:datePickerController animated:YES completion:nil];
}

#pragma mark - 年月选择器
- (void)datePickerController:(ITDatePickerController *)datePickerController didSelectedDate:(NSDate *)date dateString:(NSString *)dateString{
    if (datePickerController.tag==610) {
        YYLog(@"%@",dateString);
        
        
        NSMutableString *mut = [[NSMutableString alloc]initWithString:dateString];
        [mut insertString:@"-" atIndex:4];
        self.startTime = (NSString *)mut;
        self.startLabel.text = mut;
        
    }else{
        YYLog(@"%@",dateString);
      
//        self.endTime = dateString;
        NSMutableString *mut = [[NSMutableString alloc]initWithString:dateString];
        [mut insertString:@"-" atIndex:4];
        self.endTime = (NSString *)mut;
        self.endLabel.text = mut;
        //点完结束时间的确认后进行数据查询
        [self inqData];
    }
}

///查询工业数据
- (void)inqData{
    WeakStament(ws);
    NSDictionary *dict;
    if ([[NUserModel shareInstance].userInfoType integerValue]==1) {
        
        dict = @{@"companyId":[NUserModel shareInstance].userInfoId?:@"5",
                               @"startTime":self.startTime?:@"2018-01",
                               @"endTime":self.endTime?:@"2018-12",
                               };
    }else{
        
        //政府端
        dict = @{@"startTime":self.startTime?:@"2018-01",
                 @"endTime":self.endTime?:@"2018-12",};
    }
    
    //test  afjson  afhttp
    [NNetworkClient getRequestWithBaseUrl:kIndusDataInqUrl setParameters:dict success:^(id responseObject) {
        
        YYLog(@"%@",responseObject);
        
        if ([responseObject[@"page"] isKindOfClass:[NSNull class]]) {
            
//            [AFToast showText:@"查询结果为空"];
            
            return ;
            
        }
        NSDictionary *dict = responseObject[@"page"];
        NInqueryModel *model = [NInqueryModel mj_objectWithKeyValues:dict];
        self.output = model.industryOutput;
        [ws.dataArray addObject:model];
        [ws fillDataWithModel:model];
        
        [self.tableView reloadData];
        
    } fail:^(NSError *error) {
        
        YYLog(@"%@",error);
        
    } delegater:nil];
    
}
- (void)fillDataWithModel:(NInqueryModel *)model{
    
    self.sumLabel.text = model.industryYield;
    self.sellLabel.text = model.industrySell;
    self.exportLabel.text = model.industryExport;
}

#pragma mark - datasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([[NUserModel shareInstance].userInfoType integerValue]==2) {
        ///政府端  分全量查询   和某个企业的查询     需要一个标记
        if ([NUserModel shareInstance].isAllData) {
            
            return 0;
            
        }else{
            
            return 1;
        }
        
    }else{
        
        return 1;
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NInqueryModel *model = self.dataArray[indexPath.section];
    ///副标题不显示这样
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: cellIdentifier];
    }
    cell.textLabel.text = @"水泥制品总量";
    cell.textLabel.font = kFont15;
    cell.textLabel.textColor = kDarkGrayColor;
    cell.detailTextLabel.text = self.output;
    cell.detailTextLabel.font = kFont15;
    cell.detailTextLabel.textColor = kDarkGrayColor;
    return cell;
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    NMyQiyeModel *model = self.dataArray[indexPath.section];
    //    //    NQIyeRecruitDetailVC *detail = [NQIyeRecruitDetailVC new];
    //    //    detail.zhaopinId = model.empInfoId;
    //    //    [self.navigationController pushViewController:detail animated:YES];
    //    NMyQiyeViewController *myQi = [NMyQiyeViewController new];
    //    myQi.eid = model.enterpriseId;
    //    myQi.userType = [NUserModel shareInstance].userInfoType;
    //    [self.navigationController pushViewController:myQi animated:YES];
}


#pragma mark - section head
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(25, 0, ScreenWidth, SectionHeaderHeight)];
    if ([[NUserModel shareInstance].userInfoType integerValue]==1) {
        label.text = @"工业总产值";
    }
    [view addSubview:label];
    view.backgroundColor = kBackgroundColor;
    return view;
}

#pragma mark - section foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

@end
